import 'package:flutter/material.dart';

import '../../../common/utils/shortcut.dart';
import 'my_pop_menu.dart';
import 'my_pop_menu_item.dart';

class MyPullDownButton extends StatefulWidget {
  final Widget child;
  final List<String> itemLabels;
  final int? selectedIndex;
  final ValueChanged<int>? onSelected;

  const MyPullDownButton(
      {super.key,
      required this.child,
      required this.itemLabels,
      this.selectedIndex,
      this.onSelected});

  @override
  State<MyPullDownButton> createState() => _MyPullDownButtonState();
}

class _MyPullDownButtonState extends State<MyPullDownButton> {
  @override
  Widget build(BuildContext context) {
    return MyPopMenu(
      child: widget.child,
      builder: (context) {
        return widget.itemLabels.map((e) {
          return MyPopMenuItem(
            title: e,
            selected: widget.selectedIndex == widget.itemLabels.indexOf(e),
            onTap: () {
              back();
              if (widget.onSelected != null) {
                widget.onSelected!(widget.itemLabels.indexOf(e));
              }
            },
          );
        }).toList();
      },
    );
  }
}
